import { BrowserWindow, shell } from 'electron'
import icon from '../../../resources/icon.png?asset'
import { join } from 'path'
import { is } from '@electron-toolkit/utils'

export class TaskFrame {
	#frame!: BrowserWindow
	#width = 1200
	#height = 600

	// 创建窗口
	create() {
		this.#frame = new BrowserWindow({
			width: this.#width,
			height: this.#height,
			show: false,
			frame: false,
			autoHideMenuBar: true,
			...(process.platform === 'linux' ? { icon } : {}),
			webPreferences: {
				preload: join(__dirname, '../preload/index.js'),
				sandbox: false
			}
		})

		this.#frame.on('ready-to-show', () => {
			this.#frame.show()
		})

		this.#frame.webContents.setWindowOpenHandler((details) => {
			shell.openExternal(details.url)
			return { action: 'deny' }
		})

		if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
			// 处于开发阶段时
			this.#frame.loadURL(process.env['ELECTRON_RENDERER_URL'] + '/#/tasklist')
		} else {
			// 处于生产阶段时
			this.#frame.loadFile(join(__dirname, '../renderer/index.html'), {
				hash: '/tasklist'
			})
		}
	}
	// 判断销毁状态
	isDestroyed() {
		return this.#frame.isDestroyed()
	}
	// 让窗口获取焦点
	show() {
		this.#frame.show()
	}
	// 关闭窗口
	destroy() {
		this.#frame.destroy()
	}
}